import utils from '@/mixins/utils.js';
export default {
  name: 'edit_camera_gun',
  mixins: [utils],
  data() {
    return {
      origin_parkEquipmentEnvironmentCameraVoList: null,
      parkEnvironmentCameraList: null,
      parkEquipmentEnvironmentCameraVoList: null,
      selectOptChannelOpt: [],
      saveData: {
        addParkEquipmentEnvironmentCameraVoList: [], //新增环境枪
        deleteParkEquipmentEnvironmentCameraVoList: [], //删除环境枪
        updateParkEquipmentEnvironmentCameraVoList: [], //更新环境枪
        parkID: this.$route.query.parkId, //停车场uuid
        parkEnvironmentCameraList: [] //通道枪
      }
    };
  },
  created() {
    this.getList();
    this.getParkinglotChannels();
  },
  methods: {
    getList() {
      API.fetchGet('/operatorPark/getOperatorParkCentralMonitorResponse', {
        parkID: this.$route.query.parkId
      }).then(res => {
        this.origin_parkEquipmentEnvironmentCameraVoList = res.data.parkEquipmentEnvironmentCameraVoList ? JSON.parse(
          JSON.stringify(res.data.parkEquipmentEnvironmentCameraVoList)
        ) : []
        this.parkEquipmentEnvironmentCameraVoList = res.data.parkEquipmentEnvironmentCameraVoList ? res.data.parkEquipmentEnvironmentCameraVoList : []
        this.parkEnvironmentCameraList = res.data.parkEnvironmentCameraList ? res.data.parkEnvironmentCameraList : []
      });
    },
    //获取停车场所有通道
    getParkinglotChannels() {
      API.fetchGet(`/operatorPark/byParkID/${this.$route.query.parkId}`).then(res => {
        this.selectOptChannelOpt = res.data.map(item => {
          return {
            id: item.id,
            entranceName: item.entranceName
          };
        });
      });
    },
    addEnvirnomentGun() {
      this.parkEquipmentEnvironmentCameraVoList.unshift({
        cameraIp: '',
        channelIdList: [],
        ezvizAfterParam: '',
        ezvizParam: '',
        id: '',
        parkId: this.$route.query.parkId
      });
    },
    async delEnvironmentGunListItem(item, index) {
      const confirm_result = await this.confirmFn('您确定要删除本通道？');
      if (!confirm_result) return;

      if (item.id) {
        this.saveData.deleteParkEquipmentEnvironmentCameraVoList.push(item);
      }
      this.parkEquipmentEnvironmentCameraVoList.splice(index, 1);
    },
    confirmFn(msg) {
      return new Promise(resolve => {
        this.$confirm(msg, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          customClass: 'mgr-msg-box',
          confirmButtonClass: 'el-button el-button--danger'
        })
          .then(() => {
            resolve(true);
          })
          .catch(() => {
            resolve(false);
          });
      });
    },
    submit() {
      this.parkEquipmentEnvironmentCameraVoList.forEach(item => {
        if (!item.id) {
          this.saveData.addParkEquipmentEnvironmentCameraVoList.push(item);
        } else {
          const origin_item = this.origin_parkEquipmentEnvironmentCameraVoList.find(citem => citem.id === item.id);
          if (!this.isEq(origin_item, item)) {
            this.saveData.updateParkEquipmentEnvironmentCameraVoList.push(item);
          }
        }
      });
      this.saveData.parkEnvironmentCameraList = this.parkEnvironmentCameraList;
      API.fetchPost('/operatorPark/processOperatorParkCentralMonitorRequest', this.saveData).then(res => {
        if (res.status == 1) {
          this.showToast('保存成功！', 1);
          setTimeout(() => {
            this.back();
          }, 2000);
        } else {
          this.showToast(res.message, 2);
        }
      });
    },
    back() {
      this.$router.push({
        name: 'allParkinglotInfo',
        query: this.$route.query
      });
    },
    isEq(obj1, obj2) {
      var toString = Object.prototype.toString;

      function isFunction(obj) {
        return toString.call(obj) === '[object Function]';
      }

      function eq(a, b, aStack, bStack) {
        // === 结果为 true 的区别出 +0 和 -0
        if (a === b) return a !== 0 || 1 / a === 1 / b;

        // typeof null 的结果为 object ，这里做判断，是为了让有 null 的情况尽早退出函数
        if (a == null || b == null) return false;

        // 判断 NaN
        if (a !== a) return b !== b;

        // 判断参数 a 类型，如果是基本类型，在这里可以直接返回 false
        var type = typeof a;
        if (type !== 'function' && type !== 'object' && typeof b != 'object') return false;

        // 更复杂的对象使用 deepEq 函数进行深度比较
        return deepEq(a, b, aStack, bStack);
      }

      function deepEq(a, b, aStack, bStack) {
        // a 和 b 的内部属性 [[class]] 相同时 返回 true
        var className = toString.call(a);
        if (className !== toString.call(b)) return false;

        switch (className) {
          case '[object RegExp]':
          case '[object String]':
            return '' + a === '' + b;
          case '[object Number]':
            if (+a !== +a) return +b !== +b;
            return +a === 0 ? 1 / +a === 1 / b : +a === +b;
          case '[object Date]':
          case '[object Boolean]':
            return +a === +b;
        }

        var areArrays = className === '[object Array]';
        // 不是数组
        if (!areArrays) {
          // 过滤掉两个函数的情况
          if (typeof a != 'object' || typeof b != 'object') return false;

          var aCtor = a.constructor,
            bCtor = b.constructor;
          // aCtor 和 bCtor 必须都存在并且都不是 Object 构造函数的情况下，aCtor 不等于 bCtor， 那这两个对象就真的不相等啦
          if (
            aCtor !== bCtor &&
            !(isFunction(aCtor) && aCtor instanceof aCtor && isFunction(bCtor) && bCtor instanceof bCtor) &&
            ('constructor' in a && 'constructor' in b)
          ) {
            return false;
          }
        }

        aStack = aStack || [];
        bStack = bStack || [];
        var length = aStack.length;

        // 检查是否有循环引用的部分
        while (length--) {
          if (aStack[length] === a) {
            return bStack[length] === b;
          }
        }

        aStack.push(a);
        bStack.push(b);

        // 数组判断
        if (areArrays) {
          length = a.length;
          if (length !== b.length) return false;

          while (length--) {
            if (!eq(a[length], b[length], aStack, bStack)) return false;
          }
        }
        // 对象判断
        else {
          var keys = Object.keys(a),
            key;
          length = keys.length;

          if (Object.keys(b).length !== length) return false;
          while (length--) {
            key = keys[length];
            if (!(b.hasOwnProperty(key) && eq(a[key], b[key], aStack, bStack))) return false;
          }
        }

        aStack.pop();
        bStack.pop();
        return true;
      }
      return eq(obj1, obj2);
    }
  }
};
